<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE composition PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<ui:composition xmlns:ui="http://java.sun.com/jsf/facelets"
                template="./../../_template.xhtml"
                xmlns:h="http://xmlns.jcp.org/jsf/html"
                xmlns:p="http://primefaces.org/ui"
                xmlns:f="http://xmlns.jcp.org/jsf/core">

    <ui:define name="centro">
        <h:form>

            <p:outputLabel value="Cadastro de Documento Financeiro - Incluir" rendered="#{empty documentoBean.documento.id}" 
                           style="font-size: x-large; width: 60%; margin-left: 25px" styleClass="labelInserir" />
            <p:outputLabel value="Cadastro de Documento Financeiro - Alterar" rendered="#{not empty documentoBean.documento.id}"
                           style="font-size: x-large; width: 60%; margin-left: 25px" styleClass="labelInserir" />
            <p:fieldset styleClass="fieldsetInsert" style="width: 92%; margin-left: 25px">
                <p:messages />
                <h:panelGrid columns="5" style="margin-bottom: 10px" >
                    <h:panelGrid columns="1" >
                        <p:outputLabel style="margin-left: 25px; text-align: left" value="Conta" />
                        <p:selectOneMenu value="#{documentoBean.documento.conta}" converter="entityConverter"
                                         style="margin-left: 25px; width: 200px"
                                         required="true" requiredMessage="Selecione uma conta.">

                            <f:selectItem itemLabel="selecione" noSelectionOption="true" />
                            <f:selectItems value="#{contaBean.contas}" var="conta" itemLabel="#{conta.nome}"/>
                        </p:selectOneMenu>
                    </h:panelGrid>

                    <h:panelGrid columns="1" >
                        <p:outputLabel style="margin-left: 20px; text-align: left" value="Tipo de Documento" />
                        <p:selectOneMenu value="#{documentoBean.documento.tipoDocumento}" converter="entityConverter"
                                         required="true" requiredMessage="Selecione o tipo de documento" style="margin-left: 20px; width: 200px">
                            <f:selectItem itemLabel="selecione"/>
                            <f:selectItems value="#{tipoDocumentoBean.tipoDocumentos}" var="tipoDoc" itemLabel="#{tipoDoc.descricao}"/>
                        </p:selectOneMenu>
                    </h:panelGrid>

                    <h:panelGrid columns="1" style="margin-bottom: 5px" cellpadding="5" >
                        <p:outputLabel style="margin-left: 20px; text-align: left" value="Natureza" />
                        <p:selectOneRadio value="#{documentoBean.documento.natureza}" required="true" requiredMessage="Informe a natureza"
                                          style="margin-left: 20px">
                            <f:selectItem itemLabel="Débito"  itemValue="DEBITO" />
                            <f:selectItem itemLabel="Crédito" itemValue="CREDITO"/>
                            <p:ajax listener="#{documentoBean.ativarTipoCusto}" update="tipoCusto"/>
                        </p:selectOneRadio> 
                    </h:panelGrid>

                    <h:panelGrid columns="1" >
                        <p:outputLabel style="margin-left: 20px; text-align: left" value="Custo" />
                        <p:selectOneMenu id="tipoCusto" value="#{documentoBean.documento.tipoCusto}" disabled="#{documentoBean.habilitaTipoCusto}"
                                         required="true" requiredMessage="Selecione o tipo de custo" style="margin-left: 20px; width: 135px" >
                            <f:selectItem itemLabel="selecione"/>
                            <f:selectItems value="#{documentoBean.listaTipoCusto}" />
                        </p:selectOneMenu>
                    </h:panelGrid>

                    <h:panelGrid columns="1" >
                        <p:outputLabel style="margin-left: 20px; text-align: left" value="Grupo de Lançamento" />
                        <p:selectOneMenu value="#{documentoBean.documento.grupoLancamento}" converter="entityConverter" 
                                         style="margin-left: 20px; width: 200px" 
                                         required="true" requiredMessage="Selecione o grupo de lançamento" >
                            <f:selectItem itemLabel="selecione"/>
                            <f:selectItems value="#{grupoLancamentoBean.grupoLancamentos}" var="grupo" itemLabel="#{grupo.descricao}"/>
                        </p:selectOneMenu>
                    </h:panelGrid>
                </h:panelGrid>
                <h:panelGrid columns="4" style="margin-bottom: 10px">
                    <h:panelGrid columns="1" >
                        <p:outputLabel style="margin-left: 25px; text-align: left" value="Número" />
                        <p:inputText size="2" value="#{documentoBean.documento.nrDocumento}" 
                                     style="width: 100px; margin-left: 25px;text-align: right" 
                                     required="true" requiredMessage="Informe o número do documento" />
                    </h:panelGrid>

                    <h:panelGrid columns="1" >
                        <p:outputLabel style="margin-left: 30px; text-align: left" value="Data da Emissão" />
                        <p:calendar pattern="dd/MM/yyyy" yearRange="c-100:c+100" navigator="true" readonlyInput="true" style="margin-left: 30px;"
                                    maxdate="#{clienteBean.dataAtual}" value="#{documentoBean.documento.dataEmissao}" 
                                    required="true" requiredMessage="Informe a data de Emissão do documento" />
                    </h:panelGrid>

                    <h:panelGrid columns="1" >
                        <p:outputLabel style="margin-left: 30px; text-align: left" value="Origem/Destino" />
                        <p:selectOneMenu id="origem" value="#{documentoBean.documento.origem}" style="margin-left: 30px; width: 150px"
                                         required="true" requiredMessage="Selecione a origem do documento" >
                            <f:selectItem itemLabel="selecione"/>
                            <f:selectItems value="#{documentoBean.listaOrigens}"  var="lista" itemLabel="#{lista.origemDocumento}" />
                            <p:ajax listener="#{documentoBean.buscaNomesPorOrigem}" update="nomes"/>
                        </p:selectOneMenu>
                    </h:panelGrid>

                    <h:panelGrid columns="1" >
                        <p:outputLabel style="margin-left: 30px; text-align: left" value="Nome" />
                        <p:selectOneMenu id="nomes" value="#{documentoBean.documento.pessoa}" converter="entityConverter" 
                                         style="margin-left: 30px; width: 370px" 
                                         required="true" requiredMessage="Selecione o nome da origem do documento" >
                            <f:selectItem itemLabel="selecione"/>
                            <f:selectItems value="#{documentoBean.nomesOrigem}" var="lista" itemLabel="#{lista.nome}" />
                        </p:selectOneMenu>
                    </h:panelGrid>
                </h:panelGrid>   
                <h:panelGrid columns="6" style="margin-bottom: 10px" >
                    <h:panelGrid columns="1" >
                        <p:outputLabel style="margin-left: 25px; text-align: left" value="Valor" />
                        <p:inputText size="20" value="#{documentoBean.documento.valor}" 
                                     style="margin-left: 25px; width: 100px ;text-align: right" styleClass="moeda"
                                     required="true" requiredMessage="Informe o valor do documento" > 
                            <f:convertNumber minFractionDigits="2" maxFractionDigits="2" locale="pt_BR" />
                        </p:inputText>
                    </h:panelGrid>

                    <h:panelGrid columns="1" >
                        <p:outputLabel style="margin-left: 30px; text-align: left" value="Forma de Pagamento" />
                        <p:selectOneMenu value="#{documentoBean.documento.formaPagamento}" style="margin-left: 30px; width: 150px"
                                         required="true" requiredMessage="Selecione a forma de pagamento" >
                            <f:selectItem itemLabel="selecione"/>
                            <f:selectItem itemLabel="À vista"   itemValue="A_VISTA"  />
                            <f:selectItem itemLabel="Parcelado" itemValue="PARCELADO"/>
                            <p:ajax listener="#{documentoBean.ativarDadosParcela}" update="qtdeParcelas, periodicidade, entrada, dtPrimeiraParcela" />
                        </p:selectOneMenu>
                    </h:panelGrid>

                    <h:panelGrid columns="1" >
                        <p:outputLabel style="margin-left: 25px; text-align: left" value="Qtde Parcelas" />
                        <p:inputText id="qtdeParcelas" type="number" size="5" value="#{documentoBean.documento.qtdeParcelas}" 
                                     disabled="#{documentoBean.habilitarDadosParcela}" style="margin-left: 25px; text-align: right; width: 80px"
                                     required="true" requiredMessage="Informe a quantidade de parcelas"  />
                    </h:panelGrid>

                    <h:panelGrid columns="1" >
                        <p:outputLabel style="margin-left: 30px; text-align: left" value="Periodicidade" />
                        <p:selectOneMenu id="periodicidade" value="#{documentoBean.documento.periodicidade}" 
                                         disabled="#{documentoBean.habilitarDadosParcela}" style="margin-left: 30px" >
                            <f:selectItem itemLabel="selecione"/>
                            <f:selectItems value="#{documentoBean.listaPeriodicidade}" />
                        </p:selectOneMenu>
                    </h:panelGrid>

                    <h:panelGrid columns="1" >
                        <p:outputLabel style="margin-left: 30px; text-align: left" value="Valor de Entrada" />
                        <p:inputText id="entrada" value="#{documentoBean.documento.valorEntrada}"
                                     style="margin-left: 30px; text-align: right; width: 100px" styleClass="moeda"
                                     disabled="#{documentoBean.habilitarDadosParcela}" >
                            <f:convertNumber minFractionDigits="2" maxFractionDigits="2" locale="pt_BR" />
                        </p:inputText>
                    </h:panelGrid>

                    <h:panelGrid id="p14" columns="1" >
                        <p:outputLabel id="l4" style="margin-left: 30px; text-align: left" value="Data da Primeira Parcela" />
                        <p:calendar id="dtPrimeiraParcela" pattern="dd/MM/yyyy" yearRange="c-100:c+100" navigator="true" readonlyInput="true"
                                    mindate="#{clienteBean.dataAtual}" value="#{documentoBean.documento.dataPrimeiraParcela}" disabled="#{documentoBean.habilitarDadosParcela}"
                                    style="margin-left: 30px" />
                    </h:panelGrid>
                </h:panelGrid>
                <br/>
                <p:commandButton value="Visualizar Lançamentos" actionListener="#{documentoBean.inserirLancamento()}" ajax="false" 
                                 style="margin-left: 25px; margin-bottom: 10px" />
                <h:outputText value="Documento sem lançamentos cadastrados." rendered="#{documentoBean.exibeMsgListaVazia}"
                              style="margin-left: 25px; margin-bottom: 10px; color: blue"/> 
                <br />
                <h:panelGrid rendered="#{not empty documentoBean.documento.lancamentos}" style="margin-top: 30px" >
                    <p:fieldset style="width: 100%;" >
                        <p:dataTable value="#{documentoBean.documento.lancamentos}" var="lcto" 
                                     style="margin-left: -80px; margin-bottom: 20px; margin-top: 10px" >
                            <p:column style="width: 10px; text-align: center" headerText="Número do Lançamento"  >
                                <h:outputText value="#{lcto.sequencial}" />
                            </p:column>
                            <p:column style="width: 10px; text-align: center" headerText="Data de Pagamento" >
                                <h:outputText value="#{lcto.dataPagamento}">
                                    <f:convertDateTime pattern="dd/MM/yyyy" />
                                </h:outputText>
                            </p:column>
                            <p:column style="width: 10px; text-align: center" headerText="Valor" >
                                <h:outputText value="#{lcto.valor}" styleClass="moeda">
                                    <f:convertNumber minFractionDigits="2" maxFractionDigits="2" locale="pt_BR" />
                                </h:outputText>
                            </p:column>
                            <p:column style="width: 10px; text-align: center" headerText="Situação" >
                                <h:outputText value="#{lcto.situacao}"/>
                            </p:column>
                            <p:column style="width: 10px; text-align: center" headerText="Editar">
                                <center>
                                    <p:commandButton  icon="ui-icon-trash" action="#{documentoBean.editarLancamento(lcto)}" ajax="false"/>
                                </center>
                            </p:column>
                        </p:dataTable>
                    </p:fieldset>
                </h:panelGrid>
                <br/>
                <br/>
                <p:commandButton value="Salvar Documento" action="#{documentoBean.gravar}" style="margin-left: 25px; margin-bottom: 10px" 
                                 icon="ui-icon-check" update="@all">
                    <p:confirm header="Confirmação"  icon="ui-icon-alert"
                               message="Esta ação irá inserir os lançamentos acima no Fluxo de Caixa. Deseja continuar? "/>
                    <p:confirmDialog global="true"  showEffect="fade" >
                        <p:commandButton value="Sim" type="button" styleClass="ui-confirmdialog-yes" icon="ui-icon-check" />
                        <p:commandButton value="Não" type="button" styleClass="ui-confirmdialog-no" icon="ui-icon-close" />
                    </p:confirmDialog>
                </p:commandButton>
                <p:commandButton value="Voltar" action='#{documentoBean.voltar}' immediate="true" ajax="false" 
                                 style="margin-left: 10px; margin-bottom: 10px"  icon="ui-icon-arrowreturnthick-1-w"/>
                <p:confirmDialog global="true" showEffect="fade" id="Confirmação" >
                    <p:commandButton value="Sim" type="button" styleClass="ui-confirmdialog-yes" icon="ui-icon-check" />
                    <p:commandButton value="Não" type="button" styleClass="ui-confirmdialog-no" icon="ui-icon-close" />
                </p:confirmDialog>
            </p:fieldset> 
        </h:form>
    </ui:define>
</ui:composition>
